package cn.isaac.firstkotlin.domain

import android.util.Log
import cn.isaac.firstkotlin.adapter.ForecastResult
import com.google.gson.Gson
import java.net.URL

/**
 * Created by RaoWei on 2017/5/19 16:33.
 */
public class Request(val url: String) {
    public fun run() {
        val forecastJsonStr = URL(url).readText()
        Log.d(javaClass.simpleName, forecastJsonStr)
    }
}

public class ForecastRequest(val zipCode: String) {
    companion object {
        private val APP_ID = "15646a06818f61f7b8d7823ca833e1ce"
        private val URL  = "http://api.openweathermap.org/data/2.5/forecast/daily?mode=json&units=metric&cnt=7"
        private val COMPLETE_URL = "${URL}&APPID=${APP_ID}&q="
    }

    fun execute(): ForecastResult {
        val forecastJsonStr = URL(COMPLETE_URL + zipCode).readText()
        val gson = Gson()
        return gson.fromJson(forecastJsonStr, ForecastResult::class.java)
    }
}